/**
 * Project Name:template-web
 * File Name   :PagerUtil.java
 * Package Name:org.cn.template.component.page
 * Date:2015年9月27日下午3:38:22
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


/**
 * ClassName:PagerUtil <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午3:38:22 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	 
 */
public class PagerUtil
{
	public static final int			defaultPageRows	= 8;
	/*
	 * 请求的url..往往要在这个URL后面?参数
	 */
	private String				baseUrl;

	/*
	 * 装载的参数
	 */
	private Map<String, Object>	urlParam;

	/*
	 * 装载的数据集合
	 */
	@SuppressWarnings("rawtypes")
	private List				data;

	public interface IPager
	{
		
		public Long getListCount(Map<String,?> paramMap) throws Exception;
		/*
		 * 获取
		 */
		public List<?> getList(Map<String,?> paramMap) throws Exception;
	}
	
	
	public static void main(String[] args)
	{
		for (int i = 1; i <= 20; i++) {
			for (int j = 1; j <= defaultPageRows; j++) {
				System.out.println(i + ":" + j + ":" + JSON.toJSONString(getPageIndex(i, j)));
			}
		}
	}
	 

	public static PagerModel getPagerModel(IPager pager, Map<String, Object> paramMap) throws Exception
	{
		Integer currentPage = paramMap.get("page") == null ? 1 : Integer.parseInt(paramMap.get("page").toString());
		Integer pageRows = paramMap.get("rows") == null ? defaultPageRows : Integer.parseInt(paramMap.get("rows").toString());
		if (pageRows > 100) {
			pageRows = PagerUtil.defaultPageRows;
		}
		if (currentPage < 0) {
			currentPage = 1;
		}
		PagerModel pagerModel = new PagerModel();
		pagerModel.setPageSize(pageRows);
		pagerModel.setCurrentPage(currentPage);
		pagerModel.setCount(pager.getListCount(paramMap));

		if (pagerModel.getStartIndex() <= 0) {
			paramMap.put("rowstartindex", 0);
			paramMap.put("rowendindex", 0);
		} else {
			paramMap.put("rowstartindex", pagerModel.getStartIndex() - 1);
			paramMap.put("rowendindex", pagerModel.getPageSize());
		}
		if (pagerModel.getCount() >= 1) {
			pagerModel.setDataList(pager.getList(paramMap));
		} else {
			pagerModel.setDataList(Lists.newArrayList());
		}
		return pagerModel;
	}

	public static PagerHtmlModel getPagerHtmlModel(PagerModel pagerModel, Map<String, Object> urlParam, String baseUrl)
	{
		PagerHtmlModel htmlModel = new PagerHtmlModel();
		htmlModel.setPagerModel(pagerModel);
		htmlModel.setUrlParam(urlParam);
		htmlModel.setBaseUrl(baseUrl);
		List<PagerIndexModel> indexModels = Lists.newLinkedList();
		List<String> list = getPageIndex(pagerModel.getPageCount(),pagerModel.getCurrentPage());
		if (urlParam == null) {
			urlParam = Maps.newLinkedHashMap();
		}
		/*
		 * 拼每个页码的超链接
		 */
		for (String page : list) {
			if (NumberUtils.isNumber(page)) {
				urlParam.put("page", page);
			}
			StringBuilder builder = new StringBuilder();
			// int i = 0;
			for (Entry<String, Object> entry : urlParam.entrySet()) {
				// 对order、sort、rows不输出到页面
				if (!entry.getKey().trim().toLowerCase().equals("order")
						&& !entry.getKey().trim().toLowerCase().equals("sort")
						&& !entry.getKey().trim().toLowerCase().equals("rows")) {
					builder.append(entry.getKey()).append("=")
							.append(entry.getValue()).append("&");
				}
			}
			String temp = new String(builder);
			if (temp.endsWith("&")) {
				temp = builder.substring(0, builder.length() - 1);
			}
			indexModels.add(new PagerIndexModel(NumberUtils.isNumber(page),page,baseUrl+"?"+temp));
		}
		htmlModel.setIndexModels(indexModels);
		return htmlModel;
	}



	/**
	 * 获取分页页码集合
	 * 
	 * @param pageCount
	 * @param currentPage
	 * @return
	 */
	public static List<String> getPageIndex(long pageCount, long currentPage)
	{
		StringBuilder builder = new StringBuilder();
		int pcsize = (int) pageCount;
		int cp = (int) currentPage;
		// 总共显示数量
		int pagersize = 11;
		// 两端数量
		int betweensize = 2;
		// 中间数量
		int middlesize = 5;
		if (pcsize <= pagersize) {
			for (int i = 1; i <= pcsize; i++) {
				builder.append(i + ",");
			}
		} else {
			int betmaxsize = betweensize + middlesize;
			int endMaxSize = pcsize - betweensize - middlesize + 1;
			if (cp <= betmaxsize) {
				for (int i = 1; i <= betmaxsize + 1; i++) {
					builder.append(i + ",");
				}
				builder.append("...,");
				for (int i = pcsize - 1; i <= pcsize; i++) {
					builder.append(i + ",");
				}
			} else if (cp >= endMaxSize) {
				for (int i = 1; i <= betweensize; i++) {
					builder.append(i + ",");
				}
				builder.append("...,");
				for (int i = pcsize - betmaxsize; i <= pcsize; i++) {
					builder.append(i + ",");
				}
			} else {
				for (int i = 1; i <= betweensize; i++) {
					builder.append(i + ",");
				}
				builder.append("...,");
				for (int i = cp - betweensize; i <= cp + betweensize; i++) {
					builder.append(i + ",");
				}
				builder.append("...,");
				for (int i = pcsize - 1; i <= pcsize; i++) {
					builder.append(i + ",");
				}
			}
		}
		String[] temp = StringUtils.split(builder.toString(), ',');
		List<String> list = new ArrayList<String>();
		for (String string : temp) {
			if (string != null && !"".equals(string)) {
				list.add(string);
			}
		}
		return list;
	}

	public String getBaseUrl()
	{
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl)
	{
		this.baseUrl = baseUrl;
	}

	public Map<String, Object> getUrlParam()
	{
		return urlParam;
	}

	public void setUrlParam(Map<String, Object> urlParam)
	{
		this.urlParam = urlParam;
	}

	@SuppressWarnings("rawtypes")
	public List getData()
	{
		return data;
	}

	@SuppressWarnings("rawtypes")
	public void setData(List data)
	{
		this.data = data;
	}
}
